package com.yycx.common.mybatis.query;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.utils.JsonUtils;
import com.yycx.common.utils.ReflectionUtils;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author zyf
 */
@Data
public class CriteriaSave implements Serializable {
    private EntityMap requestMap = new EntityMap();

    private EntityMap extraMap = new EntityMap();
    /**
     * 扩展事件名称
     */
    private String handlerName;

    private Class cls;

    /**
     * 主键字段
     */
    private String idField;


    /**
     * 表名
     */
    private String tableName;

    /**
     * 对象是否先暂存到redis
     */
    private Boolean saveToRedis = false;


    public EntityMap getRequestMap() {
        return requestMap;
    }


    public String getHandlerName() {
        return handlerName;
    }

    public void setHandlerName(String handlerName) {
        this.handlerName = handlerName;
    }

    public CriteriaSave() {

    }


    public CriteriaSave(Map map, Class cls) {
        this.cls = cls;
        this.handlerName = (String) map.getOrDefault(CommonConstants.HANDLER_NAME, "");
        this.setHandlerName(handlerName);
        this.tableName = ReflectionUtils.getTableName(cls);
        this.idField = ReflectionUtils.getTableField(cls);
        this.requestMap.putAll(map);
    }


    public EntityMap put(String key, Object value) {
        this.requestMap.put(key, value);
        return this.requestMap;
    }

    public EntityMap setExtra(String key, Object value) {
        this.extraMap.put(key, value);
        return this.extraMap;
    }


    public <T> T getEntity(Class<T> t) {
        T result= JsonUtils.getEntity(t, this.requestMap);
        if(FlymeUtils.isEmpty(result)){
            try {
                result=t.newInstance();
            }catch (Exception e){
            }
        }
        return  result;
    }

    public <T> T getEntity() {
        Object obj=  BeanUtil.mapToBean(requestMap,cls,true);
        return (T) obj;
    }

    public <T> T getParams(String key) {
        return (T) requestMap.get(key);
    }


    public Boolean getBoolean(String key) {
        String val = requestMap.get(key);
        Boolean tag = false;
        if (FlymeUtils.isNotEmpty(val)) {
            if (val.equals("true") || key.equals("1")) {
                tag = true;
            }
        } else {
            tag = false;
        }
        return tag;
    }


    public <T> T getParams(String key, String def) {
        return (T) requestMap.get(key, def);
    }

    public <T> T getExtra(String key) {
        return (T) extraMap.get(key);
    }

    public List<Long> getArrayLong(String key) {
        return requestMap.getListLong(key);
    }

    public Long getLongParams(String key) {
        return requestMap.getLong(key);
    }

    public Long getLongParams(String key, Long def) {
        return requestMap.getLong(key, def);
    }

    public Long getLong(String key) {
        return requestMap.getLong(key);
    }

    public <T> T getInt(String key, Integer def) {
        return (T) requestMap.getInt(key, def);
    }

    public <T> T getRequiredParams(String key) {
        T t = requestMap.get(key);
        if (FlymeUtils.isEmpty(t)) {
            ApiAssert.failure(key + "不能为空");
        }
        return t;
    }

    public EntityMap getExtraMap() {
        return extraMap;
    }

    public void setExtraMap(EntityMap extraMap) {
        this.extraMap = extraMap;
    }
}
